import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/logic_controller.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/my/model/models.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';

class MyWithdrawPage extends StatefulWidget {
  String alipayAccount;

  MyWithdrawPage({Key? key, required this.alipayAccount}) : super(key: key);

  @override
  _MyWithdrawPageState createState() => _MyWithdrawPageState();
}

class _MyWithdrawPageState extends State<MyWithdrawPage> {
  final TextEditingController _controller = TextEditingController();
  final FocusNode _nodeText1 = FocusNode();
  List list = [];

  //最小
  String min = "0";

  //手续费
  String rate = "0";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }

  Future<void> getData() async {
    EasyLoading.show(status: "加载中");
    try {
      list = await ServiceRepository.getConfigNameDetail(
          "transfer_rate,transfer_min");
      if (list.length != 0) {
        list.forEach((v) {
          ConfigNameDetail model = v;
          if (model.name == "transfer_rate") {
            rate = model.value ?? "0";
          }
          if (model.name == "transfer_min") {
            min = model.value ?? "0";
          }
        });
      }
      EasyLoading.dismiss();
    } catch (e, s) {
      EasyLoading.showToast(http.setError(e, s));
    }
  }

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        FocusScope.of(context).requestFocus(new FocusNode());
      },
      child: Scaffold(
        appBar: MyAppBar(
          centerTitle: "提现",
        ),
        body: GetBuilder<UserController>(
            id: UserController.UserInfo,
            builder: (logic) {
              _controller.addListener(() {
                setState(() {});
              });
              return MyScrollView(
                padding: EdgeInsets.symmetric(horizontal: 15),
                children: [
                  Container(
                    height: 55,
                    decoration: BoxDecoration(
                        border: Border(
                      bottom: Divider.createBorderSide(context,
                          color: Global.kTheme.colorLine, width: 1),
                    )),
                    child: Row(
                      children: [
                        Text(
                          "到账账户",
                          style: TextStyle(
                              fontSize: 14,
                              color: Global.kTheme.textPrimary6,
                              fontFamily: Global.mySemiboldFont),
                        ),
                        Spacer(),
                        Text(
                          widget.alipayAccount,
                          style: TextStyle(
                              fontSize: 12,
                              color: Global.kTheme.inputHint1,
                              fontFamily: Global.mySemiboldFont),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: 58,
                    decoration: BoxDecoration(
                        border: Border(
                      bottom: Divider.createBorderSide(context,
                          color: Global.kTheme.colorLine, width: 1),
                    )),
                    child: Row(
                      children: [
                        Text(
                          "提现金额",
                          style: TextStyle(
                              fontSize: 14,
                              color: Global.kTheme.textPrimary6,
                              fontFamily: Global.mySemiboldFont),
                        ),
                        SizedBox(
                          width: 48,
                        ),
                        Expanded(
                          child: TextField(
                            controller: _controller,
                            keyboardType:
                                TextInputType.numberWithOptions(decimal: true),
                            decoration: InputDecoration(
                              hintText: "请输入",
                              border: InputBorder.none, // 去掉下滑线
                            ),
                          ),
                        ),
                        InkWell(
                          onTap: () {
                            setState(() {
                              _controller.text = logic.member.money ?? "0";
                            });
                          },
                          child: Text(
                            "全部提现",
                            style: TextStyle(
                              fontSize: 12,
                              color: Global.kTheme.inputHint1,
                            ),
                          ),
                        ),
                        SizedBox(
                          width: 9,
                        ),
                        Text(
                          "¥",
                          style: TextStyle(
                              fontSize: 12,
                              color: Global.kTheme.textPrimary6,
                              fontFamily: Global.mySemiboldFont),
                        ),
                      ],
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 6),
                    child: Text(
                      "可提现金额${logic.member.money ?? "0.00"}元",
                      style: TextStyle(
                          fontSize: 12, color: Global.kTheme.inputHint1),
                    ),
                  ),
                  Container(
                    height: 55,
                    decoration: BoxDecoration(
                        border: Border(
                      bottom: Divider.createBorderSide(context,
                          color: Global.kTheme.colorLine, width: 1),
                    )),
                    child: Row(
                      children: [
                        Text(
                          "手续费",
                          style: TextStyle(
                              fontSize: 14,
                              color: Global.kTheme.textPrimary6,
                              fontFamily: Global.mySemiboldFont),
                        ),
                        Spacer(),
                        Text(
                          getServiceCharge(),
                          style: TextStyle(
                              fontSize: 12,
                              color: Global.kTheme.textPrimary6,
                              fontFamily: Global.mySemiboldFont),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    height: 55,
                    decoration: BoxDecoration(
                        border: Border(
                      bottom: Divider.createBorderSide(context,
                          color: Global.kTheme.colorLine, width: 1),
                    )),
                    child: Row(
                      children: [
                        Text(
                          "实际到账",
                          style: TextStyle(
                              fontSize: 14,
                              color: Global.kTheme.textPrimary6,
                              fontFamily: Global.mySemiboldFont),
                        ),
                        Spacer(),
                        Text(
                          getArrival(),
                          style: TextStyle(
                              fontSize: 12,
                              color: Global.kTheme.textPrimary6,
                              fontFamily: Global.mySemiboldFont),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 28,
                  ),
                  Container(
                    height: 40,
                    decoration: BoxDecoration(
                      color: Global.kTheme.colorBackground3,
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: MyButton(
                      minHeight: 50,
                      onPressed: () async {
                        if (_controller.text.length == 0) {
                          return EasyLoading.showToast("请输入提现金额");
                        }

                        if (double.parse(_controller.text) <
                            double.parse(min)) {
                          return EasyLoading.showToast("最小提现金额$min");
                        }
                        EasyLoading.show(status: "加载中");
                        try {
                          await ServiceRepository.transfer(
                              money: _controller.text);
                          EasyLoading.dismiss();
                          //更新用户信息
                          LoginLogic.to.getUserInfoData();
                          Future.delayed(Duration.zero, () {
                            Get.back();
                          });
                        } catch (e, s) {
                          EasyLoading.showToast(http.setError(e, s));
                        }
                      },
                      text: "提现",
                      fontSize: 16,
                      textColor: Colors.white,
                      backgroundColor: Global.kTheme.colorBackground3,
                    ),
                  ),
                  SizedBox(
                    height: 33,
                  ),
                  Text(
                    "温馨提示",
                    style: TextStyle(fontSize: 12, color: Color(0xFF666666)),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Text(
                    "1、申请提现最低金额${min}起\n2、申请提现后，预计1-3个工作日到账，到账账户为您设置的收款账户中，请注意查收~",
                    style: TextStyle(
                        fontSize: 12, color: Global.kTheme.inputHint1),
                  )
                ],
              );
            }),
      ),
    );
  }

  String getServiceCharge() {
    try {
      return "¥${(double.parse(rate) == 0 || _controller.text.length == 0) ? "0.00" : NumUtil.multiply(double.parse(_controller.text), double.parse(rate)).toStringAsFixed(2)}";
    } catch (e) {
      return "¥0.00";
    }
  }

  String getArrival() {
    try {
      return "¥${_controller.text.length == 0 ? "0.00" : NumUtil.subtract(double.parse(_controller.text), double.parse(double.parse(rate) == 0 ? "0.00" : NumUtil.multiply(double.parse(_controller.text), double.parse(rate)).toStringAsFixed(2)))}";
    } catch (e) {
      return "¥0.00";
    }
  }
}
